package com.inboyu.application.service;

import com.inboyu.application.FeeItemApplicationSerivce;
import com.inboyu.application.request.GroupByStoreAndServiceTypeRequest;
import com.inboyu.application.response.FeeItemsResponse;
import com.inboyu.listing.domain.*;

import java.util.List;

/**
 * 费项应用编排实现
 */
public class FeeItemAppSerivceImpl implements FeeItemApplicationSerivce {

    /**
     * 房源领域服务
     */
    private final ListingDomainService listingDomainService;

    /**
     * 构造器注入
     * 
     * @param listingDomainService 房源领域服务
     */
    public FeeItemAppSerivceImpl(final ListingDomainService listingDomainService) {
        this.listingDomainService = listingDomainService;
    }

    @Override
    public FeeItemsResponse groupByStoreId(GroupByStoreAndServiceTypeRequest request) {
        // 1. 查询门店是否存在，如果门店不存在，会抛出异常：InvalidStoreException / 无效门店异常
        Store store = this.listingDomainService.findStoreById(request.storeId());
        // 2. 查询服务类型，如果服务类型不存在，会抛出异常：InvalidServiceTypeException / 无需服务类型异常
        ServiceType serviceType = this.listingDomainService.serviceType(request.serviceType());
        // 3. 根据服务类型查询费项
        List<FeeItem> feeItems = listingDomainService.findFeeItemByServiceType(request.serviceType());
        // 4. 查询门店已有的服务类型费项
        List<StoreFeeItem> storeFeeItems = listingDomainService.findStoreFeeItemByStoreIdAndServiceType(store.id(), serviceType);
        // 5. DTO 封装转化
        return FeeItemsResponse.convert(feeItems, storeFeeItems);
    }

}
